package com.caration.shell.preference;

import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;

import com.caration.sdk.utilities.DataUtils;
import com.caration.shell.R;
import com.caration.shell.ShellWallpaperManager;
import com.caration.shell.config.CommonConfig;
import com.caration.shell.cr3d.SharedData;

public class SettingWallpaperPreference {
	private static PreferenceActivity mContext;
	private static SeekBarPreference mSeekBarPref;
	private static ListPreference mListPref;

	static void onDestroy() {
		mContext = null;
		mSeekBarPref = null;
		mListPref = null;
	}

	static void init(PreferenceActivity activity) {
		mContext = activity;
		mListPref = (ListPreference) activity
				.findPreference(CommonConfig.KEY_WALLPAPER_MODE);
		String[] entries = {
				DataUtils.getString(SharedData.getContext(),
						R.string.mn_wallpaper_scroll_mode),
				DataUtils.getString(SharedData.getContext(),
						R.string.mn_wallpaper_center_mode) };
		mListPref.setEntries(entries);
		mListPref.setEntryValues(entries);
		mListPref.setDialogTitle(R.string.mn_wallpaper_mode);
		mListPref.setValueIndex(CommonConfig.getWallpaperMode());
		mListPref.setTitle(R.string.mn_wallpaper_mode);
		mListPref
				.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

					@Override
					public boolean onPreferenceChange(Preference preference,
							Object newValue) {
						int mode = ((String) newValue).equals(DataUtils
								.getString(SharedData.getContext(),
										R.string.mn_wallpaper_scroll_mode)) ? ShellWallpaperManager.SCROLL_MODE
								: ShellWallpaperManager.CENTER_MODE;
						if (CommonConfig.getWallpaperMode() != mode)
							update(mode);
						return true;
					}
				});
		setSummary();
		mSeekBarPref = (SeekBarPreference) mContext
				.findPreference(CommonConfig.KEY_WALLPAPER_TRANS_SPEED);
		mSeekBarPref.seek(ShellWallpaperManager.getSpeed());
	}

	static void update() {
		int mode = mListPref.getValue().equals(
				DataUtils.getString(SharedData.getContext(),
						R.string.mn_wallpaper_scroll_mode)) ? ShellWallpaperManager.SCROLL_MODE
				: ShellWallpaperManager.CENTER_MODE;
		if (CommonConfig.getWallpaperMode() != mode)
			update(mode);
	}

	static void update(int mode) {
		if (mSeekBarPref != null) {
			ShellWallpaperManager.setSpeed(mSeekBarPref.getProgress());
			CommonConfig.setWallpaperSpeed(ShellWallpaperManager.getSpeed());
		}
		if (mListPref != null) {
			CommonConfig.setWallpaperMode(mode);
			setSummary();
			// fm.p().b(i);TODO:excute in WallpaperManager
		}
	}

	private static void setSummary() {
		if (CommonConfig.getWallpaperMode() != ShellWallpaperManager.SCROLL_MODE)
			mListPref.setSummary(R.string.mn_wallpaper_mode_center_summary);
		else
			mListPref.setSummary(R.string.mn_wallpaper_mode_scroll_summary);
	}
}
